﻿
//return object by name of iframe by name support chrome, ie
function getObjectiframeDoc(doc, iframeName, objectName) {
        return doc.getElementById(iframeName).contentWindow.document.getElementById(objectName);
}

//return object by name of iframe by name support chrome, ie
function getObjectiframe(iframeName, objectName) {
    //alert(doc.getElementById(iframeName).window);

    //if ie , from ie <8 not have contentWindow
    if (navigator.userAgent.toLowerCase().indexOf("msie") != -1)
        return frames[iframeName].document.getElementById(objectName);
    else
        return document.getElementById(iframeName).contentWindow.document.getElementById(objectName);
}

//return object by name of iframe by name support chrome, ie
function getParentObjectiframe(iframeName, objectName) {
    //alert(parent.frames[iframeName].document.getElementById(objectName));

    //if ie 
    if (navigator.userAgent.toLowerCase().indexOf("msie") != -1)
        return parent.frames[iframeName].document.getElementById(objectName);
    else
        return parent.document.getElementById(iframeName).contentWindow.document.getElementById(objectName);
}


//function setCookie(c_name, value, exdays) {
//    var exdate = new Date();
//    exdate.setDate(exdate.getDate() + exdays);
//    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()) + "; path=/"; ;
//    document.cookie = c_name + "=" + c_value;
//}

function setCookie(c_name, value) {
    var today = new Date();
    today.setTime(today.getTime());
    var expires_date = new Date(today.getTime() + (2592000000));

    var c_value = escape(value) + "; expires=" + expires_date.toGMTString() + "; path=/";
    //var c_value = value + "; expires=" + expires_date.toGMTString() + "; path=/"; 
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    //if (c_name == 'Level1ID') alert(c_name);

    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {

            if (y == 'undefined') y = '';
//            if (c_name == 'Level1ID') alert(y);
            //alert('name: ' + c_name + '; value: ' + y);
            return unescape(y);
        }
    }
    //alert(ARRcookies);
    return null;
}

// this deletes the cookie when called
function deleteCookie(name) {
    //alert('te');
    if (getCookie(name) != null) document.cookie = name + "=;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
	

//// this deletes the cookie when called
//function Delete_Cookie(name, path, domain) {
//    if (Get_Cookie(name)) document.cookie = name + "=" +
//((path) ? ";path=" + path : "") +
//((domain) ? ";domain=" + domain : "") +
//";expires=Thu, 01-Jan-1970 00:00:01 GMT";
//}


//display object
function collapse(obj, isDisplay) {
    if (isDisplay == null) {
        if (document.getElementById(obj).style.display == "none") {
            document.getElementById(obj).style.display = "";
        }
        else {
            document.getElementById(obj).style.display = "none";
        }
    }
    else {

        if (isDisplay == false) {
            document.getElementById(obj).style.display = "none";
        }
        else {
            document.getElementById(obj).style.display = "";
        }
    }
    return false;
}
//use in list form
function showReportOnly() {
    isDisplay = document.getElementById('ctl00_chkShowGrid').checked;
    collapse('tdPages', isDisplay);
    collapse('trGrid', isDisplay);
    //if check multi sort
    if (isDisplay == true && document.getElementById('ctl00_chkSort').checked == true)
        collapse('tdSort', isDisplay);
    else
        collapse('tdSort', !isDisplay);
}

function ShowDialog(formName, width, height) {
    if (width == null || width < 100) width = Math.round((screen.width / 3) * 2) + 100; //900;
    if (width > screen.width) width = screen.width - 100; //900;

    if (height == null || height < 100) height = Math.round((screen.height / 3) * 2);
    if (height > screen.height) height = screen.height - 100; //900;

    l = 0;
    t = 0;

    //	objShowDialog = window.open(formName ,'Recipient','status=1,toolbar=0,scrollbars=1,resizable=1,alwaysRaised=Yes, top=' + t + ', left=' + l + ', width=' + width + ', height=' + height + ',1 ,align=center')	
    objShowDialog = window.open(formName, '_blank', 'status=1,toolbar=0,scrollbars=1,resizable=1, top=' + t + ', left=' + l + ', width=' + width + ', height=' + height + ',1 ,align=center')
   // objShowDialog.focus();
}
function ShowDialogReturn(formName, width, height) {
    if (width == null || width < 100) width = Math.round((screen.width / 3) * 2) + 100; //900;
    if (width > screen.width) width = screen.width - 100; //900;

    if (height == null || height < 100) height = Math.round((screen.height / 3) * 2);
    if (height > screen.height) height = screen.height - 100; //900;

    l = 0;
    t = 0;

    objShowDialog = window.open(formName, 'Recipient', 'status=1,toolbar=0,scrollbars=1,resizable=1,alwaysRaised=Yes, top=' + t + ', left=' + l + ', width=' + width + ', height=' + height + ',1 ,align=center')
   // objShowDialog.focus();
    return false;
}
function gshowModalDialog(formName, width, height) {
    if (width == null || width < 100) width = Math.round((screen.width / 3) * 2); //900;
    if (width > screen.width) width = screen.width - 100; //900;

    if (height == null || height < 100) height = Math.round((screen.height / 3) * 2);
    if (height > screen.height) height = screen.height - 100; //900;

    l = 0;
    t = 0;
    //alert(width);
    //objShowDialog = window.showModalDialog(formName ,'Recipient','status=0,toolbar=0,scrollbars=1,resizable=yes,alwaysRaised=Yes, top=' + t + ', left=' + l + ', dialogWidth=' + width + ', dialogHeight=' + height + ',1 ,align=center')	
    objShowDialog = window.showModalDialog(formName, 'Recipient', 'status=0;scroll=1;resizable=yes;alwaysRaised=Yes; dialogHeight=' + height + 'px; dialogWidth=' + width + 'px;align=center')
    //objShowDialog.focus();
}

//Phim tat: keycode = 13 # phim enter
function KeyDownHandler(strButtonName) {
    // process only the Enter key
    if (event.keyCode == 13) {
        event.returnValue = false;
        event.cancel = true;
        document.getElementById().click(strButtonName);
    }
}

//Check grid checked
function GridCheck(GridName, BeginIndex, EndIndexExt, CtlCheckName) {
    var i;
    var count;
    var NoItemCheck;

    count = document.getElementById(GridName).rows.length;
    NoItemCheck = true;
    if (count > 1) {
        for (i = BeginIndex; i < document.getElementById(GridName).rows.length + EndIndexExt; i++) {
            if (i <= 9) {
                if (document.getElementById(GridName + "_ctl0" + i + "_" + CtlCheckName).checked == true) {
                    NoItemCheck = false;
                    break;
                }
            }
            else {
                if (document.getElementById(GridName + "_ctl" + i + "_" + CtlCheckName).checked == true) {
                    NoItemCheck = false;
                    break;
                }
            }
        }
    }

    if (NoItemCheck) {
        return false;
    }
    else
        return true;
}
/////ugridcheck
function uGridCheck(GridName, BeginIndex, EndIndexExt, CtlCheckName) {
    //var value = document.getElementById(Ctl).checked;	    
    var NoItemCheck;
    var i;
    var count = igtbl_getGridById(GridName);
    var rcount = count.Rows.length;
    NoItemCheck = true;
    if (rcount > 0) {
        for (i = BeginIndex; i < rcount + EndIndexExt; i++) {
            if (document.getElementById(GridName + "_ci_0_10_" + i + "_" + CtlCheckName).checked == true) {
                NoItemCheck = false;
                break;
            }
        }
    }
    if (NoItemCheck) {
        return false;
    }
    else
        return true;
}
//Check grid checked
function GridNoHeaderCheck(GridName, BeginIndex, EndIndexExt, CtlCheckName) {
    var i;
    var count;
    var NoItemCheck;

    count = document.getElementById(GridName).rows.length;
    NoItemCheck = true;
    if (count > 0) {
        for (i = BeginIndex; i < document.getElementById(GridName).rows.length + EndIndexExt; i++) {
            if (document.getElementById(GridName + "ctl" + i + "_" + CtlCheckName).checked == true) {
                NoItemCheck = false;
                break;
            }
        }
    }

    if (NoItemCheck) {
        return false;
    }
    else
        return true;
}

//CheckAll
//Ctl: checkbox checkAll, GridCtl: Checkbox cua grid
function CheckAll(Ctl, GridName, BeginIndex, EndIndexExt, GridCtl) {
    var value = document.getElementById(Ctl).checked;
    var i;
    count = document.getElementById(GridName).rows.length;
    if (count > 1) {
        for (i = BeginIndex; i < document.getElementById(GridName).rows.length + EndIndexExt; i++) {
            if (i <= 9) {
                if (document.Form1(GridName + "_ctl0" + i + "_" + GridCtl).disabled == false) {
                    document.Form1(GridName + "_ctl0" + i + "_" + GridCtl).checked = value;
                }
            }
            else {
                if (document.Form1(GridName + "_ctl" + i + "_" + GridCtl).disabled == false) {
                    document.Form1(GridName + "_ctl" + i + "_" + GridCtl).checked = value;
                }
            }
        }
    }
}

function CheckAllWebGrid(Ctl, GridName, BeginIndex, EndIndexExt, GridCtl) {
    var value = document.getElementById(Ctl).checked;
    var i;
    var count = igtbl_getGridById(GridName);
    var rcount = count.Rows.length;
    if (rcount > 0) {
        for (i = BeginIndex; i < rcount + EndIndexExt; i++) {
            document.getElementById(GridName + "_ci_0_10_" + i + "_" + GridCtl).checked = value;
        }
    }
}
//Loai bo khoang trang
function trim(txt) {
    txt = txt.replace(/^(\s)+/, '');
    txt = txt.replace(/(\s)+$/, '');
    return txt;
}

//Ham goi su kien cua mot nut khac khi bam phim enter
function checkKey(strBtnClick, e) {

    if (window.event) // IE
    {

        if (event.keyCode == 13) {

            document.getElementById(strBtnClick).click();
            event.returnValue = false;
            event.cancel = true;
        }
    }

    else if (e.which)  // Netsccape/ Firefox /Opera
    {
        if (e.which == 13) {
            //document.getElementById(strBtnClick).click();	
            var mySplitResult = strBtnClick.split("_");
            if (mySplitResult.length > 2)
                document.getElementById(strBtnClick).onclick();
            else {
                var _name = strBtnClick.replace("_", "$");
                javascript: __doPostBack(_name, '');
            }

            //javascript:__doPostBack(strTemp,'');
            event.returnValue = false;
            event.cancel = true;

        }
    }

}

//Kiem tra kieu so phai~ la so duong
function checkNumeric(field) {

    if (trim(field.value) == "") {
        field.value = "";
        return;
    }
    var re;
    re = /,/g;
    var value = field.value.replace(re, '');
    if (isNaN(value)) {
        GetAlertError(iTotal, DSAlert, "0013");
        field.value = "";
        field.focus();
        return;
    }
    if (value < 0) {
        //alert("Must not a negative numeric or zero!");
        GetAlertError(iTotal, DSAlert, "0012");
        field.value = "";
        field.focus();
        return;
    }
    FormatNumericWithSeparate(field);
}

//Kiem tra kieu so ca~ 2 loai. so am^ - duong
function checkNumericBeta(field) {
    if (trim(field.value) == "") {
        field.value = "";
        return;
    }
    var re;
    re = /,/g;
    var value = field.value.replace(re, '');
    if (isNaN(value)) {
        GetAlertError(iTotal, DSAlert, "0013");
        field.value = "";
        field.focus();
        return;
    }

    //FormatNumericWithSeparate(field);
}

//Kiem tra kieu so 
function checkNumericNotSeparate(field) {
    if (trim(field.value) == "") {
        field.value = "";
        return;
    }
    var re;
    re = /,/g;
    var value = field.value.replace(re, '');
    if (isNaN(value)) {
        alert("Không phải là dữ liệu số!");
        field.value = "";
        field.focus();
        return;
    }
    if (value < 0) {
        //alert("Must not a negative numeric or zero!");
        alert("Dữ liệu không được là số âm!");
        field.value = "";
        field.focus();
        return;
    }
}

//Chuyen thanh dang so binh thuong cua dinh dang money: 2,000 -> 2000
function ReplaceMoneyNumeric(field) {
    return field.value.replace(/,/g, '');
}
//Chuyen thanh dang so binh thuong cua dinh dang money: 2,000 -> 2000
function ReplaceMoneyNumericFromValue(field) {
    return field.replace(/,/g, '');
}
//dinh dang kieu money ',' (ex: 2,000,000.00)
function FormatNumericWithSeparate(field) {
    var DecimalNumber = 2;
    var DecimalChar = ".";
    var SeperateChar = ",";
    var ValueString = field.value;
    var ValueStringAfterDecimal = "";
    var ValueStringNew = "";
    if (field.value < 0)
        ValueString = ValueString.substring(1, ValueString.length); //Bo dau tru
    if (ValueString.indexOf(DecimalChar) != -1) {
        ValueStringAfterDecimal = ValueString.substring(ValueString.indexOf(DecimalChar), ValueString.indexOf(DecimalChar) + 1 + DecimalNumber) // Tinh luon dau cham
        ValueString = ValueString.substring(0, ValueString.indexOf(DecimalChar))
    }

    if (ValueStringAfterDecimal.indexOf(SeperateChar) != -1) {
        alert("Không phải là dữ liệu số!");
        field.value = "";
        field.focus();
        return;
    }
    ValueString = ValueString.replace(/,/g, '');
    ValueStringNew = "";
    while (ValueString.length > 3) {
        ValueStringNew = SeperateChar + ValueString.substring(ValueString.length - 3, ValueString.length) + ValueStringNew;
        ValueString = ValueString.substring(0, ValueString.length - 3);
    }
    if (DecimalNumber != 0)
        ValueStringNew = ValueString + ValueStringNew + ValueStringAfterDecimal;
    else
        ValueStringNew = ValueString + ValueStringNew;

    if (field.value < 0)
        ValueStringNew = "- " + ValueStringNew;

    field.value = ValueStringNew;
}
//Kiem tra kieu so lam tron theo 0.5
function checkNoRound05(field) {
    if (field.value == "")
        return true;
    var value = field.value;
    if (isNaN(value)) {
        alert("Không phải là dữ liệu số!");
        field.value = "";
        field.focus();
        return false;
    }
    if (value <= 0) {
        alert("Dữ liệu không được là số âm hoặc bằng 0!");
        field.value = "";
        field.focus();
        return false;
    }
    if (Math.round(parseFloat(value) / 0.5) != (parseFloat(value) / 0.5)) {
        alert("Số hợp lệ phải là bội số của 0.5 ");
        field.value = "";
        field.focus();
        return false;
    }
    return true;
}

//Kiem tra kieu so lam tron theo 0.5 (0, 0.5, 1)
function DaycheckNoRound05(field) {
    if (field.value == "")
        return true;
    var value = field.value;
    if (isNaN(value)) {
        alert("Không phải là dữ liệu số!");
        field.value = "";
        field.focus();
        return false;
    }
    if (parseFloat(value) != 0 && parseFloat(value) != 0.5
		&& parseFloat(value) != 1) {
        alert("Số hợp lệ là số có 1 trong 3 giá trị: 0; 0.5; 1");
        field.value = "";
        field.focus();
        return false;
    }
    field.value = parseFloat(value);
    return true;
}

//Kiem tra so phan tram
function CheckPercent(field) {
    if (field.value == "")
        return;
    var value = field.value;
    if (isNaN(value)) {
        alert("Không phải là dữ liệu số!");
        field.value = "";
        field.focus();
        return;
    }
    if (value < 0 || value > 100) {
        alert("Số phải nằm trong khoảng từ 0 đến 100!");
        field.value = "";
        field.focus();
        return;
    }
}

//Kiem tra so nguyen
function checkInteger(field) {
    var value = field.value;
    if (isNaN(value)) {

        alert("Số không hợp lệ!");
        field.focus();
        field.value = "";
        return false;
    }
    else {
        if (value < 0) {
            alert("Số không được là số âm!");
            field.focus();
            field.value = "";
            return false;
        }
        else {
            field.value = Math.round(field.value);
            return true;
        }
    }
}
//Kiem tra gio phut dinh dang(hh:mm)
function formatHours(field) {
    var nSep1, sHour, sMinute, nHour, nMinute;
    var checkstr = "0123456789";
    var DataTemp = "";
    var DataValue = "";
    var value, i;
    DataValue = field.value;
    if (DataValue.length == 0) {
        field.value = "";
        return false;
    }
    //Bo nhung ky tu khong phai ky tu so
    for (i = 0; i < DataValue.length; i++) {
        if (checkstr.indexOf(DataValue.substr(i, 1)) >= 0) {
            DataTemp = DataTemp + DataValue.substr(i, 1);
        }
    }
    if (DataTemp.length != 4) {
        field.value = "";
        return false;

    }
    sHour = DataTemp.substring(0, 2);
    sMinute = DataTemp.substring(2, 4);
    if (isNaN(sHour) || isNaN(sMinute)) {
        field.value = "";
        return false;
    }

    nHour = parseInt(sHour, 10); nMinute = parseInt(sMinute, 10);
    if (nHour < 0 || nMinute < 0) {
        field.value = "";
        return false;
    }
    if (nHour >= 24 || nMinute >= 60) {
        field.value = "";
        return false;
    }
    value = sHour + ":" + sMinute;
    field.value = value;
    return true;
}
//Kiem tra nam
function CheckYear(field) {
    var checkstr = "0123456789";
    var DateField = field;
    var DateValue = "";
    var DateTemp = "";
    var i;

    DateValue = DateField.value;
    if (DateValue.length == 0) return;
    for (i = 0; i < DateValue.length; i++) {
        if (checkstr.indexOf(DateValue.substr(i, 1)) >= 0) {
            DateTemp = DateTemp + DateValue.substr(i, 1);
        }
    }
    DateValue = DateTemp;

    if (DateValue.length == 2)
        DateValue = "20" + DateValue;

    if ((DateValue.length != 4 && DateValue.length != 0) || DateValue == 0) {
        alert("Năm không hợp lệ!");
        DateField.focus();
    }
    DateField.value = DateValue;
}

//Kiem tra du lieu thang nam
function CheckMonthYear(field) {
    var checkstr = "0123456789";
    var DateField = field;
    var Datevalue = "";
    var DateTemp = "";
    var seperator = "/";
    var month;
    var year;
    var leap = 0;
    var err = 0;
    var i;
    err = 0;
    DateValue = DateField.value;
    if (DateValue == "") return;
    /* Delete all chars except 0..9 */
    for (i = 0; i < DateValue.length; i++) {
        if (checkstr.indexOf(DateValue.substr(i, 1)) >= 0) {
            DateTemp = DateTemp + DateValue.substr(i, 1);
        }
    }
    DateValue = DateTemp;
    /* Always change date to 6 digits - string*/
    /* if year is entered as 2-digit / always assume 20xx */
    if (DateValue.length == 4) {
        DateValue = DateValue.substr(0, 2) + '20' + DateValue.substr(2, 2);
    }

    if (DateValue.length != 6) {
        err = 19;
    }
    /* year is wrong if year = 0000 */
    year = DateValue.substr(2, 4);
    if (year == 0) {
        err = 20;
    }
    /* Validation of month*/
    month = DateValue.substr(0, 2);
    if ((month < 1) || (month > 12)) {
        err = 21;
    }

    /* if 00 ist entered, no error, deleting the entry */
    if ((month == 0) && (year == 00)) {
        err = 0; month = ""; year = ""; seperator = "";
    }

    if (err == 0) {
        DateField.value = month + seperator + year;
        if (DateField.value == "") {
            alert("Định dạng dữ liệu kiểu MM/yyyy!");
            DateField.focus();
            // HOPTD ADDED
            return false;
            //////////////
        }
    }
    /* Error-message if err != 0 */
    else {
        DateField.value = "";
        DateField.focus();
        alert("Định dạng dữ liệu kiểu MM/yyyy!");
        // HOPTD ADDED
        return false;
        //////////////
    }
}

//Kiem tra du lieu ngay
function CheckDate(field) {
    var checkstr = "0123456789";
    var DateField = field;
    var Datevalue = "";
    var DateTemp = "";
    var seperator = "/";
    var day;
    var month;
    var year;
    var leap = 0;
    var err = 0;
    var i;
    err = 0;
    DateValue = DateField.value;
    /* Delete all chars except 0..9 */
    for (i = 0; i < DateValue.length; i++) {
        if (checkstr.indexOf(DateValue.substr(i, 1)) >= 0) {
            DateTemp = DateTemp + DateValue.substr(i, 1);
        }
    }
    DateValue = DateTemp;
    /* Always change date to 8 digits - string*/
    /* if year is entered as 2-digit / always assume 20xx 
    HungDD edit : if year is entered as 2-digit / if 2 digits of year =< 70 assume 20xx / else assume 19xx
    */
    if (DateValue.length == 6) {
        var str = DateValue.substr(4, 2);
        //alert("str : " + str);
        var y = 0;
        if (str == "08") y = 8;
        else if (str == "09") y = 9;
        else
            y = parseInt(str);
        //alert("y : " + y);

        var strYear = y < 10 ? "0" + y : "" + y;

        //alert("strYear : " + strYear);
        if (y > 70)
            DateValue = DateValue.substr(0, 4) + '19' + strYear;
        else
            DateValue = DateValue.substr(0, 4) + '20' + strYear;
    }

    if (DateValue.length != 8) {
        //hungdd edit 05/05/2009
        /*replace '/' & '-' & '.' to '/' */
        var strDate = DateField.value;
        var currentDate = new Date();
        var reg = /\d/;
        var pattern = /\/{1,}/g;
        if (strDate.indexOf(".") > -1 || strDate.indexOf("-") > -1) {
            strDate = strDate.replace(/\./g, "/");
            strDate = strDate.replace(/-/g, "/");
            strDate = strDate.replace(pattern, "/");
            var sDate = strDate.substring(0, strDate.indexOf("/"));
            strDate = strDate.replace(sDate + "/", "");
            var sMonth = strDate.substring(0, strDate.indexOf("/"));
            strDate = strDate.replace(sMonth + "/", "");
            var sYear = strDate.substring(strDate.length - 2, strDate);
            if (sMonth == null || sMonth == "") {
                sMonth = sYear;
                sYear = currentDate.getFullYear();
            }
            //alert(sDate + ";" + sMonth + ";" + sYear);
            if (reg.test(sDate) == true && reg.test(sMonth) == true && reg.test(sYear) == true) {
                /*Compile to DateTime string*/
                sDate = sDate.length == 1 ? "0" + sDate : sDate;
                sMonth = sMonth.length == 1 ? "0" + sMonth : sMonth;
                var intYear = parseInt(sYear);
                switch (sYear.length) {
                    case 1:
                        sYear = "200" + sYear;
                        break;
                    case 2:
                        sYear = intYear <= 70 ? "20" + sYear : "19" + sYear;
                        break;
                }
                //alert(date + ";" + month + ";" + year);
                DateValue = sDate + sMonth + sYear;
            }
            else
                err = 19;
        }
        //end : hungdd edit 05/05/2009
        else {
            if (reg.test(strDate) == true) {
                var intDate = parseInt(strDate);
                var myDate = new Date();
                myDate.setFullYear(currentDate.getFullYear(), currentDate.getMonth() + 1, 1);
                myDate.setDate(myDate.getDate() - 1);
                if (intDate > myDate.getDate() || intDate <= 0)
                    err = 25;
                else {
                    DateValue = intDate < 10 ? "0" + strDate : strDate;
                    var month_ = currentDate.getMonth() + 1;
                    DateValue += month_ < 10 ? "0" + month_ : "" + month_;
                    DateValue += currentDate.getFullYear();
                }
            }
            else
                err = 19;
        }
    }
    //end HungDD edit 
    /* year is wrong if year = 0000 */
    year = DateValue.substr(4, 4);
    if (year == 0) {
        err = 20;
    }
    /* Validation of month*/
    month = DateValue.substr(2, 2);
    if ((month < 1) || (month > 12)) {
        err = 21;
    }
    /* Validation of day*/
    day = DateValue.substr(0, 2);

    if (year <= 1900) {
        err = 18;
    }
    if (day < 1) {
        err = 22;
    }
    /* Validation leap-year / february / day */
    if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
        leap = 1;
    }
    if ((month == 2) && (leap == 1) && (day > 29)) {
        err = 23;
    }
    if ((month == 2) && (leap != 1) && (day > 28)) {
        err = 24;
    }
    /* Validation of other months */
    if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
        err = 25;
    }
    if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
        err = 26;
    }
    /* if 00 ist entered, no error, deleting the entry */
    if ((day == 0) && (month == 0) && (year == 00)) {
        err = 0; day = ""; month = ""; year = ""; seperator = "";
    }
    /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
    if (err == 0) {
        DateField.value = day + seperator + month + seperator + year;
        return true;
    }
    else if (err = 18)/* Error-message if err != 0 */
    {
        DateField.value = "";
        DateField.focus();
        GetAlertError(iTotal, DSAlert, "0030");
        return false;
    }
    else {
        DateField.value = "";
        DateField.focus();
        GetAlertError(iTotal, DSAlert, "0031");
        return false;
    }
}

function AddDaystoDate(value, count) {
    //chi ap dung cho count <=28
    var day = value.substr(0, 2);
    var month = value.substr(3, 2);
    var year = value.substr(6, 4);
    day = parseFloat(day) + count;
    /* Validation leap-year / february / day */
    leap = 0;
    if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
        leap = 1;
    }
    if ((month == 2) && (leap == 1) && (day > 29)) {
        month = parseFloat(month) + 1;
        day = parseFloat(day) - 29;
    }
    else if ((month == 2) && (leap != 1) && (day > 28)) {
        month = parseFloat(month) + 1;
        day = parseFloat(day) - 28;
    }
    /* Validation of other months */
    else if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {

        if (month == "12") {
            month = 1;
            year = parseFloat(year) + 1;
        } else
            month = parseFloat(month) + 1;
        day = day - 31;
    }
    else if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
        month = month + 1;
        day = day - 30;
    }
    if ((day + '').length == 1)
        day = "0" + day;
    if ((month + '').length == 1)
        month = "0" + month;
    return day + "/" + month + "/" + year;
}

function AddMonthstoDate(value, count) {
    var day = value.substr(0, 2);
    var month = value.substr(3, 2);
    var year = value.substr(6, 4);
    count = parseFloat(count);
    month = parseFloat(month) + parseFloat(count);
    var delta = 0;
    while (month - 12 > 0) {
        delta = parseFloat(delta) + 1;
        month = month - 12;
    }
    year = parseFloat(year) + delta;
    if ((day + '').length == 1)
        day = "0" + day;
    if ((month + '').length == 1)
        month = "0" + month;
    return day + "/" + month + "/" + year;

}

function AddYearstoDate(value, count) {
    var day = value.substr(0, 2);
    var month = value.substr(3, 2);
    var year = value.substr(6, 4);
    count = parseFloat(count);
    year = parseFloat(year) + parseFloat(count);
    if (day.length == 1)
        day = "0" + day;
    if (month.length == 1)
        month = "0" + month;
    return day + "/" + month + "/" + year;
}

//Chuyen du lieu ngay dang dd/mm/yyyy thanh mm/dd/yyyy
function ConvertMMDDYYYY(field) {
    if (CheckDate(field) == false)
        return;
    var checkstr = "0123456789";
    var seperator = "/";
    var day;
    var month;
    var year;
    var i;
    var value = field.value;
    var valueTemp = "";
    /* Delete all chars except 0..9 */
    for (i = 0; i < value.length; i++) {
        if (checkstr.indexOf(value.substr(i, 1)) >= 0) {
            valueTemp = valueTemp + value.substr(i, 1);
        }
    }
    value = valueTemp;
    year = value.substr(4, 4);
    month = value.substr(2, 2);
    day = value.substr(0, 2);
    return month + seperator + day + seperator + year;
}

function DeltaTwoDay(value1, value2) {
    if (value1 == "" || value2 == "")
        return;
    var checkstr = "0123456789";
    var seperator = "/";
    var i;
    var valueTemp1 = "";
    var valueTemp2 = "";
    /* Delete all chars except 0..9 */
    for (i = 0; i < value1.length; i++) {
        if (checkstr.indexOf(value1.substr(i, 1)) >= 0) {
            valueTemp1 = valueTemp1 + value1.substr(i, 1);
        }
    }
    for (i = 0; i < value2.length; i++) {
        if (checkstr.indexOf(value2.substr(i, 1)) >= 0) {
            valueTemp2 = valueTemp2 + value2.substr(i, 1);
        }
    }
    value1 = valueTemp1;
    value2 = valueTemp2;
    year1 = value1.substr(4, 4);
    month1 = value1.substr(2, 2);
    day1 = value1.substr(0, 2);
    year2 = value2.substr(4, 4);
    month2 = value2.substr(2, 2);
    day2 = value2.substr(0, 2);
    var delta = Date.UTC(parseInt(year2, 10), parseInt(month2, 10) - 1, parseInt(day2, 10)) - Date.UTC(parseInt(year1, 10), parseInt(month1, 10) - 1, parseInt(day1, 10));
    return ((delta / 86400000) + 1)
}

function DeltaDate1Date2(field1, field2) {
    if (CheckDate(field1) == false)
        return;
    if (CheckDate(field2) == false)
        return;
    var value1 = field1.value;
    var value2 = field2.value;
    if (value1 == "" || value2 == "")
        return;
    var checkstr = "0123456789";
    var seperator = "/";
    var i;
    var valueTemp1 = "";
    var valueTemp2 = "";
    /* Delete all chars except 0..9 */
    for (i = 0; i < value1.length; i++) {
        if (checkstr.indexOf(value1.substr(i, 1)) >= 0) {
            valueTemp1 = valueTemp1 + value1.substr(i, 1);
        }
    }
    for (i = 0; i < value2.length; i++) {
        if (checkstr.indexOf(value2.substr(i, 1)) >= 0) {
            valueTemp2 = valueTemp2 + value2.substr(i, 1);
        }
    }
    value1 = valueTemp1;
    value2 = valueTemp2;
    year1 = value1.substr(4, 4);
    month1 = value1.substr(2, 2);
    day1 = value1.substr(0, 2);
    year2 = value2.substr(4, 4);
    month2 = value2.substr(2, 2);
    day2 = value2.substr(0, 2);
    var delta = Date.UTC(parseInt(year2, 10), parseInt(month2, 10) - 1, parseInt(day2, 10)) - Date.UTC(parseInt(year1, 10), parseInt(month1, 10) - 1, parseInt(day1, 10));
    return ((delta / 86400000) + 1)
}

/*********************************************
*****  Fromdate < ToDate return True    ******
**  Duoc Call boi function FromSmallToDate ***
*********************************************/
function IsSmaller(inputStr1, inputStr2) {
//    alert(inputStr1);
//    alert(inputStr2);

    var delim1 = inputStr1.indexOf("/")
    var delim2 = inputStr1.lastIndexOf("/")

    // lay ngay, thang, nam cua ngay1
    var dd1 = parseInt(inputStr1.substring(0, delim1), 10)
    var mm1 = parseInt(inputStr1.substring(delim1 + 1, delim2), 10)
    var yyyy1 = parseInt(inputStr1.substring(delim2 + 1, inputStr1.length), 10)

    delim1 = inputStr2.indexOf("/")
    delim2 = inputStr2.lastIndexOf("/")

    // lay ngay, thang, nam cua ngay2
    dd2 = parseInt(inputStr2.substring(0, delim1), 10)
    mm2 = parseInt(inputStr2.substring(delim1 + 1, delim2), 10)
    yyyy2 = parseInt(inputStr2.substring(delim2 + 1, inputStr2.length), 10)

    // ngay1 nho hon ngay 2 ?
    if (yyyy2 > yyyy1)
        return true;
    else if (yyyy2 == yyyy1) {
        if (mm2 == mm1)
            if (dd2 >= dd1) return true;
            else return false;
        else
            if (mm2 > mm1) return true;
            else return false;
    }
    else
        return false;
}
/******************************************
**** Kiem tra tu ngay nho hon Den ngay ****
**** objFromDate : Thanh phan cua form ****
**** objToDate : Thanh phan cua form   ****
******************************************/
function FromSmallToDate(objFromDate, objToDate) {
    FromDate = objFromDate.value
    ToDate = objToDate.value
    if ((!isBlank(FromDate)) && (!isBlank(ToDate))) {
        if (!IsSmaller(FromDate, ToDate)) {
            //alert("FromDate must be less than ToDate!");
            //objToDate.focus()
            return false;
        }
        else
            return true;
    }
    else
        return true;
}
/******************************************
**** Kiem tra ngay nho hon ngay hien tai ****
**** objFromDate : Thanh phan cua form ****
******************************************/
function FromSmallNow(objFromDate) {
    FromDate = objFromDate.value
    var now = new Date();
    var sMonth = (now.getMonth() + 1).toString();
    var sDay = now.getDate().toString();
    var sYear = now.getFullYear().toString();
    ToDate = sDay + '/' + sMonth + '/' + sYear;

    if ((!isBlank(FromDate)) && (!isBlank(ToDate))) {

        if (!IsSmaller(FromDate, ToDate)) {
            return false;
        }
        else
            return true;
    }
    else
        return true;
}
/******************************************
**** Kiem tra ngay nho hon ngay hien tai ****
**** objFromDate : Thanh phan cua form ****
******************************************/
function FromSmallNowMonth(objFromMonth) {
    FromDate = '01/' + objFromMonth.value
    var now = new Date();
    var sMonth = (now.getMonth() + 1).toString();
    var sDay = now.getDate().toString();
    var sYear = now.getFullYear().toString();
    ToDate = sDay + '/' + sMonth + '/' + sYear;

    if ((!isBlank(FromDate)) && (!isBlank(ToDate))) {
        if (!IsSmaller(FromDate, ToDate)) {
            return false;
        }
        else
            return true;
    }
    else
        return true;
}
/******************************************
**** Kiem tra thang nho hon thang den ****
**** objFromDate : Thanh phan cua form ****
******************************************/
function FromSmallToMonth(objFromMonth, objToMonth) {
    //alert(objToMonth.value)
    var ToDate, FromDate
    FromDate = '01/' + objFromMonth.value
    if (objToMonth.value == '')
        ToDate = '01/01/2100'
    else
        ToDate = '01/' + objToMonth.value

    if ((!isBlank(FromDate)) && (!isBlank(ToDate))) {
        if (!IsSmaller(FromDate, ToDate)) {
            //alert("FromDate must be less than ToDate!");
            //objToDate.focus()
            return false;
        }
        else
            return true;
    }
    else
        return true;
}
function CheckQuarter(field) {
    if (checkNumeric(field) == false)
        return false;
    value = field.value;
    if (value != "1" && value != "2" && value != "3" && value != "4") {
        alert("Quarter is invalid!");
        field.focus();
        return false;
    }
}

function Confirm_Delete() {

    if (confirm("Những dòng được chọn sẽ bị xoá?"))
        return true;
    else
        return false;
}
function EmailCheck(obj) {
    emailStr = obj.value;
    var emailPat = /^(.+)(.+)$/;
    var specialChars = "\\(\\)<>,;:\\\\\\\"\\.\\[\\]";
    var validChars = "\[^\\s" + specialChars + "\]";
    var quotedUser = "(\"[^\"]*\")";
    var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom = validChars + '+';
    var word = "(" + atom + "|" + quotedUser + ")";
    var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");
    var matchArray = emailStr.match(emailPat);

    if (matchArray == null) {
        alert("Địa chỉ email không hợp lệ (check  and .'s)");
        obj.value = "";
        return false;
    }

    var user = matchArray[1];
    var domain = matchArray[2];

    if (user.match(userPat) == null) {
        alert("Tên người dùng không hợp lệ");
        obj.value = "";
        return false;
    }

    var IPArray = domain.match(ipDomainPat);

    if (IPArray != null) {
        for (var i = 1; i <= 4; i++) {
            if (IPArray[i] > 255) {
                alert("Destination IP address is invalid!");
                obj.value = "";
                return false;
            }
        }
        return true;
    }

    var domainArray = domain.match(domainPat);
    if (domainArray == null) {
        alert("The domain name doesn't seem to be valid.");
        obj.value = "";
        return false;
    }

    var atomPat = new RegExp(atom, "g");
    var domArr = domain.match(atomPat);
    var len = domArr.length;
    if (domArr[domArr.length - 1].length < 2 ||
		domArr[domArr.length - 1].length > 3) {
        alert("The address must end in a three-letter domain, or two letter country.");
        obj.value = "";
        return false;
    }

    if (len < 2) {
        var errStr = "This address is missing a hostname!";
        obj.value = "";
        alert(errStr);
        return false;
    }
    return true;
}

//Show calendar
function ShowCalendar(field, object) {
    //document.all[field].value = window.showModalDialog('Calendar.htm',document.all[field].value, 'dialogLeft:450px;dialogTop:300px;dialogHeight:210px;dialogWidth:265px;center:No;help:No;scroll:No;resizable:No;status:No;');
    displayCalendar(document.getElementById(field), 'dd/mm/yyyy', object);
    //return false;

}

function CheckMonthYear(field) {
    var checkstr = "0123456789";
    var DateField = field;
    var Datevalue = "";
    var DateTemp = "";
    var seperator = "/";
    var month;
    var year;
    var leap = 0;
    var err = 0;
    var i;
    err = 0;
    DateValue = DateField.value;
    if (DateValue == "") return;
    /* Delete all chars except 0..9 */
    for (i = 0; i < DateValue.length; i++) {
        if (checkstr.indexOf(DateValue.substr(i, 1)) >= 0) {
            DateTemp = DateTemp + DateValue.substr(i, 1);
        }
    }
    DateValue = DateTemp;
    /* Always change date to 6 digits - string*/
    /* if year is entered as 2-digit / always assume 20xx */
    if (DateValue.length == 4) {
        DateValue = DateValue.substr(0, 2) + '20' + DateValue.substr(2, 2);
    }

    if (DateValue.length != 6) {
        err = 19;
    }
    /* year is wrong if year = 0000 */
    year = DateValue.substr(2, 4);
    if (year == 0) {
        err = 20;
    }
    /* Validation of month*/
    month = DateValue.substr(0, 2);
    if ((month < 1) || (month > 12)) {
        err = 21;
    }

    /* if 00 ist entered, no error, deleting the entry */
    if ((month == 0) && (year == 00)) {
        err = 0; month = ""; year = ""; seperator = "";
    }

    if (err == 0) {
        DateField.value = month + seperator + year;
        if (DateField.value == "") {
            alert("Định dạng dữ liệu kiểu: mm/yyyy!");
            DateField.focus();
        }
    }
    /* Error-message if err != 0 */
    else {
        DateField.value = "";
        DateField.focus();
        alert("Định dạng dữ liệu kiểu: mm/yyyy!");
    }
}
// -- report 
function ShowReport(report, sql, param, value, path) {
    t = screen.height - 30;
    w = screen.width - 10;
    sql = GetParamReport(sql);
    //FileWindow = window.open(path + '/Reports/COMM_ReportGen.asp?report=' + report + '&sql=' + sql + '&formula=' + param + '&Value=' + value ,'_blank','width=' + w + ', height=' + t + ', left=0, top=0, location=no, scrollbars=yes,resizable');								
    //alert('http://localhost/iHRP/Reports/COMM_ReportGen.asp?report=' + report + '&sql=' + sql + '&formula=' + param + '&Value=' + value);
    alert('http://localhost/iHRP/Reports/COMM_ReportGen.asp?report=' + report + '&sql=' + sql + '&formula=' + param + '&Value=' + value);

    FileWindow = window.open('http://localhost/iHRP/Reports/COMM_ReportGen.asp?report=' + report + '&sql=' + sql + '&formula=' + param + '&Value=' + value, '_blank', 'width=' + w + ', height=' + t + ', left=0, top=0, location=no, scrollbars=yes,resizable');
    FileWindow.focus();
    return true;
}
function GetParamReport(str) {
    for (i = 0; i < window.document.forms(0).length - 1; i++) {
        obj = window.document.forms(0).item(i);

        if (str.indexOf(obj.id) != -1 && obj.id != '') {

            str = str.replace(obj.id, 'N[' + obj.value);
        }
    }
    while (str.indexOf('"') != -1) {
        str = str.replace('"', "'");
        str = str.replace("'N[", "N'");
    }
    return str;
}
function GetAlertError(iTotal, obj, sMess) {
    
    var sReturnValue = "";
    var i;
    for (i = 0; i <= iTotal; i++) {
        if (obj[0][i] == sMess) {
            alert(obj[1][i]);
            return;
        }
    }

}
function GetAlertText(iTotal, obj, sMess) {
    var sReturnValue = "";
    var i;
    for (i = 0; i <= iTotal; i++) {
        if (obj[0][i] == sMess) {
            return obj[1][i];
            break;
        }

    }
}

var lastColor;

function DG_changeBackColor(row, highlight) {

    if (highlight) {
        row.style.cursor = "hand";
        lastColor = row.style.backgroundColor;
        row.style.backgroundColor = '#DFEEF1'; //87cefa
    }
    else
        row.style.backgroundColor = lastColor;
}
// hung added
function checkMoney(field) {
    var value = field.value;


    if (value.indexOf(".", value.indexOf(".") + 1) != -1) {
        //alert("Numeric can not have more than one characters \".\" !");
        alert("Số không thể có nhiều hơn một dấu \".\" !");
        field.value = "";
        field.focus();
        return;
    }

    value = value.replace(",", "");
    value = value.replace(",", "");
    value = value.replace(",", "");
    value = value.replace(",", "");
    value = value.replace(",", "");
    value = value.replace(",", "");
    value = value.replace(",", "");
    value = value.replace(",", "");
    value = value.replace(",", "");
    value = value.replace(",", "");
    value = value.replace(",", "");

    if (isNaN(value)) {
        //alert("Not a numeric!");
        alert("Bạn phải nhập số!");
        field.value = "";
        field.focus();
        return;
    }

    if (value < 0) {
        //alert("Must not a negative numeric!");
        alert("Phải là số không âm!");
        field.value = "";
        field.focus();
        return;
    }

    FormatNumericWithSeparate(field);
}

function ShowMenu(strFunctionID) {
    //alert(parent.document.getElementById(strFunctionID));

    var menuLink = parent.document.getElementById(strFunctionID);

    LinkClick(menuLink);

    return false;
}

function LinkClick(object) {
    //alert(parent.document.getElementById(strFunctionID));

    if (object != null) {
        if (navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
            object.click();
        } else { // from question link in comment
//            object.click();
            //alert(object.id);
            var event = document.createEvent("HTMLEvents");
            event.initEvent("click", true, true);
            object.dispatchEvent(event);
        }

    }
}